﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Json;
using System.Security.Cryptography;

namespace httpclienttest
{
    public partial class Form1 : Form
    {
        HttpClient client = new HttpClient();

        MD5 md5;

        public Form1()
        {
            InitializeComponent();




        }


        // 委托
        delegate void HttpCallbackDelete(   String response, Object sender);


        private void HttpCallback(String response, Object sender)
        {
            this.richTextBox1.AppendText(response);
        }
 

        private void Bpost_Click(object sender, EventArgs e)
        {

            String url = this.Taddr.Text.ToString();
            Dictionary<string, string> pdata = new Dictionary<string, string>();
            HttpContent content;

            try{

                //计算 md5  59.41.62.130:27001/api/uploaddata/set/10000accessToken 3DA03ABFB66FB4CC984D9B32B1733122 DetailsDatactimestampb04220AC90B1247A1B01B64F36C42DA96
                String Data = Taddr.Text.Substring(7)+ TaccessTokenName.Text + TaccessTokenValue.Text
                    + TDetailsDataName.Text + TDetailsDataValue.Text
                    + TtimestampName.Text + TtimestampValue.Text
                    + TsignatureKey.Text;
                
                md5 = new MD5CryptoServiceProvider();

                //Data = Data.Replace(" ", "");

                byte[] fdata = System.Text.Encoding.UTF8.GetBytes(Data);
               byte[] bData = md5.ComputeHash(fdata);
               TsignatureValue.Text = BitConverter.ToString(bData).Replace("-", "");
                content = new FormUrlEncodedContent(new Dictionary<string, string>()       
                {    {TaccessTokenName.Text,TaccessTokenValue.Text},
                     {TDetailsDataName.Text,TDetailsDataValue.Text},
                     {TtimestampName.Text,TtimestampValue.Text},
                     {TsignatureName.Text,TsignatureValue.Text}
                 });
            }
            catch
            {


                content = new FormUrlEncodedContent(new Dictionary<string, string>()       
                {    {"Id","6"},
                     {"Name","kaihua"},
                     {"Info", "this.is test......"}//键名必须为空
                 });
            }

            try
            {

                client.PostAsync(url, content).ContinueWith(
                    (requestTask) =>
                    {
                        try
                        {
                            // Get HTTP response from completed task.                    
                            HttpResponseMessage response = requestTask.Result;
                            // Check that response was successful or throw exception                    
                            response.EnsureSuccessStatusCode();
                            // Read response asynchronously as JsonValue                    
                            //and write out top facts for each country  
                            response.Content.ReadAsStreamAsync().ContinueWith(
                                (readTask) =>
                                {
                                    //foreach (var country in readTask.Result)                            
                                    //{                            	                                
                                    //    Console.WriteLine(" {0}, Country Code: {1}," 
                                    //        + " Capital: {2}, Latitude: {3}, Longitude: {4}",                                    
                                    //        country.Value["name"],                                   
                                    //        country.Value["iso2Code"],                                    
                                    //        country.Value["capitalCity"],                                   
                                    //        country.Value["latitude"],                                    
                                    //        country.Value["longitude"]);                            
                                    //}   
                                    byte[] buffer = new byte[1000];
                                    readTask.Result.Read(buffer, 0, (int)readTask.Result.Length);
                                    String ss = System.Text.Encoding.Default.GetString(buffer);
                                    Console.Write(ss);

                                    if (this.richTextBox1.InvokeRequired)
                                    { 
                                        HttpCallbackDelete callback = new HttpCallbackDelete(HttpCallback);
                                        this.richTextBox1.BeginInvoke(callback, new Object[] { ss, this });
                                    }else{
                                        this.richTextBox1.AppendText(ss);
                                    }
                                });
                        }
                        catch
                        {
                            //this.richTextBox1.AppendText("response nul.\n");
                        }
                    });
            }
            catch { }
            Console.ReadLine();  



        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Clear();
        }
    }
}
